//
//  LoadResigter_ViewController.m
//  MagicBox
//
//  Created by dllo on 16/8/11.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LoadResigter_ViewController.h"
#import "EaseConversationListViewController.h"  // 对话框


@interface LoadResigter_ViewController ()<UITextFieldDelegate, EMChatManagerDelegate>

@property(nonatomic, retain) UILabel *userNameLabel;
@property(nonatomic, retain) UITextField *userNameTextField;
@property(nonatomic, retain) UILabel *passWordLabel;
@property(nonatomic, retain) UITextField *passWordTextField;
@property(nonatomic, retain) UILabel *affirmPassWordLabel;
@property(nonatomic, retain) UITextField *affirmPassWordTextField;
@property(nonatomic, retain) UIButton *autoLoad;

@property(nonatomic, assign) BOOL isAutoLoad;

@end

@implementation LoadResigter_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    NSLog(@"%d", self.isAutoLoad);
    [self createSubs];
    

}


-(void)createSubs{
    
    UIButton *xButton = [[UIButton alloc]initWithFrame:CGRectMake(SPACED * 3, SPACED * 2, SPACED * 2, SPACED * 2)];
    [xButton setBackgroundImage:[UIImage imageNamed:@"x.png"] forState:UIControlStateNormal] ;
    [self.view addSubview:xButton];
    [xButton addTarget:self action:@selector(backTo:) forControlEvents:UIControlEventTouchUpInside];
    
    // 用户名
    self.userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREENWIDTH / 4, 100, SCREENWIDTH / 6, SCREENHEIGHT / 20)];
    self.userNameLabel.text = @"用户名";
    self.userNameLabel.textAlignment = 1;
    [self.view addSubview:_userNameLabel];
    
    self.userNameTextField = [[UITextField alloc]init];
    self.userNameTextField.backgroundColor = AUTOCOLOR;
    self.userNameTextField.delegate = self;
    [self.view addSubview:_userNameTextField];
    [self.userNameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.size.mas_equalTo(CGSizeMake(SCREENWIDTH / 3, SCREENHEIGHT / 20));
        make.top.equalTo(self.userNameLabel.mas_top);
        make.left.equalTo(self.userNameLabel.mas_right).offset(SPACED * 2);
        
    }];
    
    
    // 密码
    self.passWordLabel = [[UILabel alloc]init];
    self.passWordLabel.text = @"密码";
    self.passWordLabel.textAlignment = 1;
    [self.view addSubview:_passWordLabel];
    [self.passWordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.size.mas_equalTo(CGSizeMake(SCREENWIDTH / 6, SCREENHEIGHT / 20));
        make.top.equalTo(self.userNameLabel.mas_bottom).offset(SPACED * 2);
        make.left.equalTo(self.view.mas_left).offset(SCREENWIDTH / 4);
        
    }];
    
    self.passWordTextField = [[UITextField alloc]init];
    self.passWordTextField.backgroundColor = AUTOCOLOR;
    self.passWordTextField.delegate = self;
    [self.view addSubview:_passWordTextField];
    [self.passWordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(SCREENWIDTH / 3, SCREENHEIGHT / 20));
        make.top.equalTo(self.passWordLabel.mas_top);
        make.left.equalTo(self.passWordLabel.mas_right).offset(SPACED * 2);
        
    }];
    
    
    self.autoLoad = [UIButton buttonWithType:UIButtonTypeCustom];
    _autoLoad.backgroundColor = AUTOCOLOR;
    _autoLoad.frame = CGRectMake(180, 230, [Tools getTextWidth:@"是否自动登录" withFontSize:20], SPACED * 2);
   
    if ([EMClient sharedClient].isAutoLogin) {
     
        [_autoLoad setTitle:@"当前始终保持登录" forState:UIControlStateNormal];
        self.isAutoLoad = YES;
        
    }else{
   
        [_autoLoad setTitle:@"是否自动登录" forState:UIControlStateNormal];
        self.isAutoLoad = NO;
    }
    
    [self.view addSubview:_autoLoad];
    [self.autoLoad addTarget:self action:@selector(autoLoad:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    registerButton.frame = CGRectMake(100, 300, 100, 50);
    registerButton.backgroundColor = AUTOCOLOR;
    
    [registerButton setTitle:@"注册" forState:UIControlStateNormal];
    
    [self.view addSubview:registerButton];
    [registerButton addTarget:self action:@selector(registerButton:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    loginButton.frame = CGRectMake(250, 300, 100, 50);
    loginButton.backgroundColor = AUTOCOLOR;
    
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    
    [self.view addSubview:loginButton];
    [loginButton addTarget:self action:@selector(loadButton:) forControlEvents:UIControlEventTouchUpInside];

    
    
}

-(void)autoLoad:(UIButton *)aotuButton{
   

    _isAutoLoad = !_isAutoLoad;
    NSLog(@"%d", _isAutoLoad);
    if (self.isAutoLoad) {
        [[EMClient sharedClient].options setIsAutoLogin:YES];
        [self.autoLoad setTitle:@"当前始终登录" forState:UIControlStateNormal];
        
    }else{
        [[EMClient sharedClient].options setIsAutoLogin:NO];
        [self.autoLoad setTitle:@"是否自动登录" forState:UIControlStateNormal];
    }
    
    
    
}


-(void)backTo:(UIButton *)button{
    
    [self dismissViewControllerAnimated:YES completion:^{
       
        NSLog(@"我退回去了");
    }];
}



-(void)sendMessageButton:(UIButton *)button{
    
    
    EaseConversationListViewController *listVC = [[EaseConversationListViewController alloc]init];
    listVC.title = @"会话列表";
    UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:listVC];
    [self presentViewController:navi animated:YES completion:nil];
    
    
    
    NSLog(@"接收到了消息");
    // 接收消息
    // 注册消息回调
    [[EMClient sharedClient].chatManager addDelegate:self delegateQueue:nil];
    
    
    
    
}

// 接收到消息
-(void)didReceiveMessages:(NSNotification *)notification
{
    NSLog(@"是我么?");
}



-(void)registerButton:(UIButton *)button{
    
    // 注册
    EMError *error = [[EMClient sharedClient] registerWithUsername:self.userNameTextField.text password:self.passWordTextField.text];
    if (error == nil) {
        NSLog(@"@注册成功@");
        UIAlertController *registerAlert = [UIAlertController alertControllerWithTitle:@"恭喜" message:@"注册成功" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *completeAction = [UIAlertAction actionWithTitle:@"直接登录" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
           
            
            EMError *loadError = [[EMClient sharedClient]loginWithUsername:self.userNameTextField.text password:self.passWordTextField.text];
            if (!loadError) {
                NSLog(@"登录成功");
                
                [[NSUserDefaults standardUserDefaults] setObject:self.userNameTextField.text forKey:@"userName"];
                [[NSUserDefaults standardUserDefaults] setObject:self.passWordTextField.text forKey:@"userPassWord"];
                
                [self dismissViewControllerAnimated:YES completion:nil];
                
            }
            
            
            
        }];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        
        [registerAlert addAction:completeAction];
        [registerAlert addAction:cancelAction];
        [self presentViewController:registerAlert animated:YES completion:^{
            
        }];
        
        
    }else{
        NSLog(@"注册失败%@, %u", error.errorDescription, error.code);
    }
    
    
}



-(void)loadButton:(UIButton *)button{
    
    if (self.userNameTextField.text != nil && self.passWordTextField.text != nil)
    {
        
        EMError *loadError = [[EMClient sharedClient] loginWithUsername:self.userNameTextField.text password:self.passWordTextField.text];
         if (!loadError)
         {
    
             [[NSUserDefaults standardUserDefaults] setObject:self.userNameTextField.text forKey:@"userName"];
             [[NSUserDefaults standardUserDefaults] setObject:self.passWordTextField.text forKey:@"userPassWord"];
             
             [self dismissViewControllerAnimated:YES completion:nil];
             
          }
        else
          {
        
        NSLog(@"登录失败: %@, %u",loadError.errorDescription, loadError.code);
        }
    
    }
    
}

-(void)didAutoLoginWithError:(EMError *)aError{
    //添加回调监听代理: [[EMClient sharedClient] addDelegate:self delegateQueue:nil];
    /*!
     *  自动登录返回结果
     *
     *  @param aError 错误信息
     */
    NSLog(@"自动登录错误信息:%@",aError);
}


-(void)didConnectionStateChanged:(EMConnectionState)aConnectionState{
    
    NSLog(@"重新连接: %d", aConnectionState);
    
    
}


-(void)textFieldDidEndEditing:(UITextField *)textField{
    [self resignFirstResponder];
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
